﻿@inherits AdminCompontentBase
<MCard Class="@($"d-flex flex-column px-2 rounded-xl line-fill {(AlarmRule.IsEnabled?"":"fill-disabled")}")" MinHeight=Height Style="@($"{(AlarmRule.IsEnabled?"":"opacity:0.6")}")">
    <MCardTitle class="px-0 py-1">
        <MListItem Class="overflow-hidden">
            <MListItemTitle Class="emphasis--text h7 text-truncate">
                @AlarmRule.DisplayName
            </MListItemTitle>
            <MListItemIcon Class="mr-n2">
                <MMenu OffsetY>
                    <ActivatorContent>
                        <MButton Class="mr-4" Icon>
                            <SIcon @attributes="@context.Attrs" Size=20 Color="emphasis2">mdi-dots-vertical</SIcon>
                        </MButton>
                    </ActivatorContent>
                    <ChildContent>
                        <MList Dense>
                            <MListItem Link OnClick="HandleEdit">
                                <MListItemContent>
                                    <MListItemTitle>
                                        <span class="body regular--text">@T("Edit")</span>
                                    </MListItemTitle>
                                </MListItemContent>
                            </MListItem>
                            <MListItem Link Href="@($"/alarmHistory/{AlarmRule.Id}")">
                                <MListItemContent>
                                    <MListItemTitle>
                                        <span class="body regular--text">@T("AlarmHistory")</span>
                                    </MListItemTitle>
                                </MListItemContent>
                            </MListItem>
                            @if (AlarmRule.IsEnabled)
                            {
                                <MListItem Link OnClick="@(()=>ConfirmAsync(T("DisableConfirmation"), HandleDisable))">
                                    <MListItemContent>
                                        <MListItemTitle>
                                            <span class="body regular--text">@T("Disable")</span>
                                        </MListItemTitle>
                                    </MListItemContent>
                                </MListItem>
                            }
                            @if (!AlarmRule.IsEnabled)
                            {
                                <MListItem Link OnClick="@(()=>ConfirmAsync(T("EnabledConfirmation"), HandleEnabled))">
                                    <MListItemContent>
                                        <MListItemTitle>
                                            <span class="body regular--text">@T("Enabled")</span>
                                        </MListItemTitle>
                                    </MListItemContent>
                                </MListItem>
                            }
                        </MList>
                    </ChildContent>
                </MMenu>

            </MListItemIcon>
        </MListItem>
    </MCardTitle>
    <MCardText class="flex-grow-1 px-4 d-flex flex-column">
        <div class="subtitle2 mt-1 d-flex">
            @if (AlarmRule.Type == AlarmRuleTypes.Log)
            {
                <MChip IsActive Color="emphasis" Class="rounded-3 body2 default-tag mt-3" TextColor="white">
                    @AlarmRule.ProjectIdentity
                </MChip>
                <MChip Class="rounded-3 body2 default-tag ml-3 mt-3" Color="emphasis2" Outlined>
                    @AlarmRule.AppIdentity
                </MChip>
            }
            @if (AlarmRule.Type == AlarmRuleTypes.Metric)
            {
                foreach (var item in _metricMonitorItems)
                {
                    <MChip Class="rounded-3 body2 default-tag mr-3 mt-3" Color="emphasis2" Outlined>
                        <EllipsisText>@item.Aggregation.Name</EllipsisText>
                    </MChip>
                }
            }
        </div>
    </MCardText>
    <MCardActions class="px-3 pb-3">
        @if (AlarmRule.Notifiers.Any())
        {
            <SIcon Tooltip="@T("Notifier")" Size="20" Class="mr-2">mdi-bell</SIcon>
            <div class="avatar-list-stacked">
                @foreach (var item in AlarmRule.Notifiers)
                {
                    <MTooltip Top>
                    <ActivatorContent>
                        <MAvatar Size="24" @attributes="@context.Attrs">
                            <MImage Src="@item.Avatar"></MImage>
                        </MAvatar>
                    </ActivatorContent>
                    <ChildContent>
                        <span>@item.DisplayName</span>
                    </ChildContent>
                </MTooltip>
                }
            </div>
            
        }
        <MSpacer />
        <span class="caption regular3--text">@AlarmRule.ModificationTime.Humanize(culture:I18n.Culture) </span>
        <span class="d-flex px-1" style="height: 8px">
            <MDivider Vertical />
        </span>
        <span class="caption regular--text">@AlarmRule.ModifierName</span>
    </MCardActions>
</MCard>

@code {
    [Parameter]
    public int Height { get; set; } = 168;

    [EditorRequired]
    [Parameter]
    public AlarmRuleListViewModel AlarmRule { get; set; } = null!;

    [Parameter]
    public EventCallback OnEdit { get; set; }

    private List<MetricMonitorItemViewModel> _metricMonitorItems => AlarmRule.MetricMonitorItems.Where(x => !string.IsNullOrEmpty(x.Aggregation.Name)).Take(5).ToList();

    AlarmRuleService AlarmRuleService => AlertCaller.AlarmRuleService;

    private async Task HandleEdit()
    {
        if (OnEdit.HasDelegate)
        {
            await OnEdit.InvokeAsync();
        }
    }

    private async Task HandleDisable()
    {
        await AlarmRuleService.SetIsEnabledAsync(AlarmRule.Id, false);
        AlarmRule.IsEnabled = false;
    }

    private async Task HandleEnabled()
    {
        await AlarmRuleService.SetIsEnabledAsync(AlarmRule.Id, true);
        AlarmRule.IsEnabled = true;
    }
}